package net.zjitc.queue;

public class ArrayQueue<T> implements MyQueue<T> {
    private int front, rear, max = 20;//front 前段指针 rear 后端指针 max 数组最大长度
    private Object[] arr;

    public ArrayQueue(int max) {
        this.max = max;
        arr = new Object[max];
    }

    public ArrayQueue() {
    }

    @java.lang.Override
    public boolean isEmpty() {
        return front == rear ? true : false;
    }

    @java.lang.Override
    public boolean add(T x) {
        if (isFull()) {
            throw new RuntimeException("队列已经满");
        }
        arr[rear] = x;
        rear = (rear + 1) % max;
        return true;
    }

    @java.lang.Override
    public T poll() {
        T var = (T) arr[front];
        arr[front] = null;
        front = (front + 1) % max;
        return (T) arr[front];
    }

    @java.lang.Override
    public T peek() {
        return (T) arr[front];
    }

    public boolean isFull() {
        return (rear + 1) % max == front ? true : false;
    }

    //打印
    public void print() {

    }
}
